package br.com.dao;

import java.util.ArrayList;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;



import br.com.connection.Connection;
import br.com.model.Login;
import br.com.model.Usuario;

public class LoginDao {
	
	public static Login consultarLogin(String username){
		Login login = new Login();
		
		EntityManager manager = Connection.criarConexao();
		
		manager.getTransaction().begin();
		CriteriaBuilder cb = manager.getCriteriaBuilder();
		CriteriaQuery<Login> cl = cb.createQuery(Login.class);
		Root<Login> rl = cl.from(Login.class);
		cl.select(rl);
		Predicate predicate = cb.equal(rl.get("username"), username);	
		cl.where(predicate);
		TypedQuery<Login> query = manager.createQuery(cl);
		
		
		try{
			login = query.getSingleResult();
			manager.getTransaction().commit();
			manager.close();
		}catch(NoResultException ex){
			return null;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		
		
		return login;
		
	}
	
	public static Login consultarPorUsuario(int id){
		Login login = new Login();
		
		EntityManager manager = Connection.criarConexao();
		
		manager.getTransaction().begin();
		CriteriaBuilder cb = manager.getCriteriaBuilder();
		CriteriaQuery<Login> cl = cb.createQuery(Login.class);
		Root<Login> rl = cl.from(Login.class);
		cl.select(rl);
		Predicate predicate = cb.equal(rl.get("usuario"), id);	
		cl.where(predicate);
		TypedQuery<Login> query = manager.createQuery(cl);
		
		try{
			login = query.getSingleResult();
			manager.getTransaction().commit();
			manager.close();
		}catch(NoResultException ex){
			return null;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		
		
		return login;
		
	}
	
	public static boolean cadastrarLogin(Login login){
		EntityManager manager = Connection.criarConexao();
		try{
			
			manager.getTransaction().begin();
			manager.persist(login);
			manager.getTransaction().commit();
			manager.close();
		}catch(Exception ex){
			System.out.println("Erro: "+ex);
			return false;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}
	
	public static boolean removerLogin(Login login, int id){
		EntityManager manager = Connection.criarConexao();
		try{
			
			manager.getTransaction().begin();
			Login l = manager.find(Login.class, id);
			l.setPerfil(login.getPerfil());
			l.setSenha(login.getSenha());
			l.setUsername(login.getUsername());
			l.setUsuario(login.getUsuario());
			l.setIsAtivo(login.getIsAtivo());
			manager.getTransaction().commit();
			manager.close();
		}catch(Exception ex){
			System.out.println("Erro: "+ex);
			return false;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
		
	}
	
	public static boolean editarLogin(Login login){
		EntityManager manager = Connection.criarConexao();
		try{
			
			manager.getTransaction().begin();
			manager.merge(login);
			manager.getTransaction().commit();
			manager.close();
		}catch(Exception ex){
			System.out.println("Erro: "+ex);
			return false;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}
	
	public static ArrayList<Login> listarLogin(){
		ArrayList<Login> lista = new ArrayList<>();
		EntityManager manager = Connection.criarConexao();
		try {			
			
			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Login> cl = cb.createQuery(Login.class);
			Root<Login> rl = cl.from(Login.class);
			cl.select(rl);
			Predicate predicate = cb.equal(rl.get("isAtivo"), 1);
			cl.where(predicate);
			TypedQuery<Login> query = manager.createQuery(cl);
			lista = (ArrayList<Login>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();
		
		} catch (Exception e) {
			System.out.println("Erro"+e);
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return lista;
	}

}
